﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CO2FixCalibration
{
    class WriteModelMeas
    {
        static int[] GetExtremeTimes(DataSet[] Measurements)
        {
            int Max = int.MinValue;
            int Min = int.MaxValue;

            foreach (DataSet D in Measurements)
            {
                if (D.FirstTime < Min) Min = D.FirstTime;
                if (D.LastTime > Max) Max = D.LastTime;
            }
            
            return new int[2] { Min, Max };
        }

        static int[] GetExtremeTimes(DataSet[] Measurements, DataSet[] Simulations)
        {
            int Max = int.MinValue;
            int Min = int.MaxValue;

            foreach (DataSet D in Measurements)
            {
                if (D.FirstTime < Min) Min = D.FirstTime;
                if (D.LastTime > Max) Max = D.LastTime;
            }
            foreach (DataSet D in Simulations)
            {
                if (D.FirstTime < Min) Min = D.FirstTime;
                if (D.LastTime > Max) Max = D.LastTime;
            }
            return new int[2] { Min, Max };
        }
        public static void Write(DataSet[] Reps, string FileName)
        {
            List<string> Content = new List<string>();
            string hdr = "t\t";
            foreach (DataSet d in Reps)
            {
                hdr += d.Label + "md\t";
            }
            Content.Add(hdr);
            int[] Years = GetExtremeTimes(Reps);
            for (int t = Years[0]; t < Years[1]; t++)
            {
                string line = t + "\t";

                for (int d = 0; d < Reps.Length; d++)
                {
                    line += Reps[d].GetValueAtTimeAsString(t) + "\t";
                }
                Content.Add(line);
            }


            System.IO.File.WriteAllLines(FileName, Content.ToArray());
        }
        public static void Write(DataSet[] Measurements, DataSet[] Simulations, string FileName )
        {
            List<string> Content = new List<string>();
            string hdr = "t\t";
            foreach(DataSet d in Measurements)
            {
                hdr += d.Label + "md\t" + d.Label + "ms\t";
            }
            Content.Add(hdr);
            int[] Years = GetExtremeTimes(Measurements, Simulations);
            for (int t = Years[0]; t < Years[1]; t++)
            {
                string line = t + "\t";

                for (int d = 0; d < Measurements.Length; d++)
                {
                    line += Simulations[d].GetValueAtTimeAsString(t) + "\t" + Measurements[d].GetValueAtTimeAsString(t) + "\t";
                }
                Content.Add(line);
            }


            System.IO.File.WriteAllLines(FileName, Content.ToArray());
        }
    }
}
